home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-25 | 3.7 KB | 75 lines | [TEXT/GEOL] |
- Item 2673838 20-June-90 18:16PDT
-
- From: ROSENSTEIN1 Rosenstein, Larry
-
- To: MM.XOBJ MacroMind, Haim Zamir,PRT
-
- cc: CPLUS.APPLE$ C++ Interest List--Apple Employees
- CPLUS.DEV$ C++ Interest List--Developers
-
- Sub: Re: Global memory and…
-
- I have to start with the disclaimer that I'm not an expert on the C++ runtime.
- I think I know how it works, and I tried out a few simple programs. Hopefully,
- someone more knowledgable will point out any mistakes.
-
- First, if you stick to using descendants of PascalObject then the runtime is
- the same as Object Pascal. Object Pascal was designed to optimize space at the
- expense of method dispatching time. You also can use the dispatching
- optimizations built into the linker. It turns out that you can use many of
- C++'s features with Object Pascal (constructors, non-virtual functions, pure
- virtual functions, to name 3).
-
- If you use C++ native classes, then the question is whether the class has any
- virtual members or not. If the class is not intended to be subclassed (which
- is occasionally valid), then a class is the same as a struct and member
- function calls are the same as any function call.
-
- Once you have a virtual function in the class, then things start to get
- interesting. Each class with one or more virtual functions has one or more
- vtables. The vtables contain information about the virtual functions of the
- class. Each instance of a class has pointers to its vtables. (You can have >1
- vtable in a class when multiple inheritance is involved.)
-
- The vtables are in global space, and are initialized as part of the standard C
- global initialization. However, the only direct reference to a vtable is from
- a global variable, which is also an initialized global.
-
- Consequently, I don't think it is necessary that the vtables be located within
- 32K of A5, although the pointer must be. A big C++ program may exceed the 32K
- limit on the global data segment, but if you use the -ss option to increase the
- segment size and the -srt option to sort the global data (putting the vtables
- farther from the start of the segment) then you won't have a problem.
-
- If you have a base class B and make a derived class D, then the vtable for D
- will be a copy of the vtable for B, with D-specific methods added to the end.
- You can see that if you have many classes, the space for vtables can add up. I
- think each entry in the vtable is 8 bytes long. (This has do to C++'s emphasis
- on speed over space. Copying the method table means that dispatching is a
- constant-time operation.)
-
- The vtable contains a pointer to the actual method, and on the Macintosh, this
- must be a pointer to a jump table entry. There is a limit of 4096 (or so) jump
- table entries, right now. (In Object Pascal, methods that aren't overridden
- are not referenced from any method table, and don't necessarily need a jump
- table entry.)
-
- Finally, a C++ method call requires about 16 bytes of code, compared to Object
- Pascal which requires 4. I think this is partly because C++ code is translated
- into C, which is then compiled. It's also a result of trying to make
- dispatching as fast as possible.
-
- You can reduce the space requirements somewhat by using subclasses of
- SingleObject (or HandleObject). In MPW C++, this provides single inheritance
- only, using a simpler runtime implementation. In this case, each entry in the
- vtable is 4 bytes, and a method call is about 10 bytes.
-
- I hope this information helps. As I said, I'm not an expert in the C++
- runtime, so you may want to experiment with C++ for yourself. You can use the
- -c option to CPlus to dump out the generated C code, and generate link maps to
- see how global data space is used.
-
- Larry Rosenstein
-
-
-